package com.firefly.service.command.mc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.mc.po.McMenuPo;
import com.firefly.dao.dao.mc.po.McRolePermissionPo;
import com.firefly.dao.dao.mc.po.McRolePo;
import com.firefly.dao.dao.mc.service.McMenuService;
import com.firefly.util.handle.FireflyException;
import com.firefly.dao.assembler.mc.McRoleAssembler;
import com.firefly.dao.assembler.mc.McRolePermissionAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.mc.McRoleDto;
import com.firefly.dao.dto.mc.McRoleMenuDto;
import com.firefly.service.repository.mc.McRolePermissionRepository;
import com.firefly.service.repository.mc.McRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @description:
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2023/1/31
 **/
@Service
public class McRoleCommand {
    private final McRoleAssembler roleAssembler;
    private final McRolePermissionAssembler rolePermissionAssembler;
    private final McMenuService menuService;
    private final McRoleRepository roleRepository;
    private final McRolePermissionRepository rolePermissionRepository;
    @Autowired
    public McRoleCommand(McRoleRepository roleRepository, McRolePermissionRepository rolePermissionRepository, McRolePermissionAssembler rolePermissionAssembler, McRoleAssembler roleAssembler, McMenuService menuService){
        this.roleAssembler = roleAssembler;
        this.rolePermissionAssembler = rolePermissionAssembler;
        this.roleRepository = roleRepository;
        this.rolePermissionRepository = rolePermissionRepository;
        this.menuService = menuService;
    }
    public Boolean create(McRoleDto request){
        McRolePo entity = roleAssembler.getPo(request);
        return roleRepository.create(entity);
    }
    //	更新单条名称
    public Boolean update(McRoleDto request) {
        McRolePo entity = roleAssembler.getPo(request);
        return roleRepository.update(entity);
    }
    //	更新单条状态
    public Boolean updateStatus(McRoleDto request) {
        McRolePo entity = roleAssembler.getPo(request);
        return roleRepository.updateStatus(entity);
    }
    //	删除单条,同时删除关联的权限信息
    @Transactional
    public Boolean remove(CommonDto request) throws Exception {
        int roleId = request.detailId;
        // 判断是不是创始人
        if (roleId == 1) {
            throw new FireflyException(403, "超级管理员角色不能删除");
        }
        rolePermissionRepository.removePrivByRoleId(roleId);
        return roleRepository.removeById(roleId);
    }
    @Transactional
    public Boolean removeMultiple(CommonDto request) throws Exception {
        List<Integer> roleIds = request.detailIds;
        // 判断是不是创始人
        if (roleIds.contains(1)) {
            throw new FireflyException(403, "超级管理员角色不能删除");
        }
        // 同时删除关联的权限信息
        rolePermissionRepository.removePrivByRoleId(roleIds);
        return roleRepository.removeById(roleIds);
    }
    // 修改权限
    @Transactional
    public Boolean postPermission(McRoleMenuDto request) {
        // 删除旧的
        rolePermissionRepository.removePrivByRoleId(request.getId());
        // 将查出来的数据组成数组插入到role_permission表里
        List<Integer> menuIds = request.getMenuIds();
        if (menuIds.size() > 0) {
            QueryWrapper<McMenuPo> listWrapper = new QueryWrapper<>();
            listWrapper.in("id", menuIds);
            List<McMenuPo> all = menuService.list(listWrapper);
            Integer roleId = request.getId();
            List<McRolePermissionPo> inserts = rolePermissionAssembler.getListPo(roleId,all);
            rolePermissionRepository.insertRolePermission(inserts);
        }
        return true;
    }
}
